using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Emlyn.Common.Data.SqlServer.SqlManagers;
using Emlyn.Common.Data.SqlServer.DataLayer;

namespace Emlyn.Website.Users
{
    public class EWebsiteDataLayer: SimpleTableDataLayer<EWebsite, string>
    {
        public DataSet GetWebsiteByUserID(Guid aUserID)
        {
            string lSql =
                "select w.* " +
                "from tblEOWebsite w " +
                "join tblEOUser u " +
                "on w.WebsiteCode = u.WebsiteCode " +
                "where u.UserID=@UserID ";
            SqlParameter lUserIDParameter = new SqlParameter("@UserID", SqlDbType.UniqueIdentifier);
            lUserIDParameter.Value = aUserID;
            List<SqlParameter> lParameters = new List<SqlParameter>();
            lParameters.Add(lUserIDParameter);

            return SqlManager.GetDataSet(lSql, lParameters);
        }

        //public void AddWebsiteUserRelationship(string aWebsiteCode, Guid aUserID)
        //{
        //    string lSql =
        //        "insert tblEOUser_Website (User_WebsiteID, UserID, WebsiteCode) " +
        //        "values (@User_WebsiteID, @UserID, @WebsiteCode) ";

        //    SqlParameter lUser_WebsiteIDParameter = new SqlParameter("@User_WebsiteID", SqlDbType.UniqueIdentifier);
        //    lUser_WebsiteIDParameter.Value = Guid.NewGuid();
        //    SqlParameter lUserIDParameter = new SqlParameter("@UserID", SqlDbType.UniqueIdentifier);
        //    lUserIDParameter.Value = aUserID;
        //    SqlParameter lWebsiteCodeParameter = new SqlParameter("@WebsiteCode", aWebsiteCode);

        //    List<SqlParameter> lParameters = new List<SqlParameter>();

        //    lParameters.Add(lUser_WebsiteIDParameter);
        //    lParameters.Add(lUserIDParameter);
        //    lParameters.Add(lWebsiteCodeParameter);

        //    SimpleSQL.Execute(lSql, lParameters);
        //}

        //public void DeleteWebsiteUserRelationship(string aWebsiteCode, Guid aUserID)
        //{
        //    string lSql =
        //        "delete tblEOUser_Website " +
        //        "where UserID=@UserID and WebsiteCode=@WebsiteCode ";

        //    SqlParameter lUserIDParameter = new SqlParameter("@UserID", SqlDbType.UniqueIdentifier);
        //    lUserIDParameter.Value = aUserID;
        //    SqlParameter lWebsiteCodeParameter = new SqlParameter("@WebsiteCode", aWebsiteCode);

        //    List<SqlParameter> lParameters = new List<SqlParameter>();

        //    lParameters.Add(lUserIDParameter);
        //    lParameters.Add(lWebsiteCodeParameter);

        //    SimpleSQL.Execute(lSql, lParameters);
        //}

        protected override string TableName()
        {
            return "tblEOWebsite";
        }

        protected override string KeyName()
        {
            return "WebsiteCode";
        }

        protected override IList<string> NonKeyAttributeNamesWritable()
        {
            List<string> retval = new List<string>();

            retval.Add("DBVersion");

            return retval;
        }

        protected override IList<string> NonKeyAttributeNamesReadonly()
        {
            List<string> retval = new List<string>();

            return retval;
        }

        protected override string DoGetKey(EWebsite aInstance)
        {
            return aInstance.WebsiteCode;
        }

        protected override object GetParameterValue(EWebsite aInstance, string aParameterName)
        {
            switch (aParameterName)
            {
                case "DBVersion": return aInstance.Version;
            }

            throw new ApplicationException("Unknown Parameter Requested: " + aParameterName);
        }
    }
}
